<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder=""></el-input>
			</el-form-item>
			<el-form-item label="工作地点" prop="workplace">
				<el-input v-model="dataForm.workplace" style="width: 100%" placeholder="请输入" />
			</el-form-item>
			<el-form-item label="国家地区" prop="regionCode">
				<el-tree-select v-model="dataForm.regionCode" :data="dictionariesList" value-key="id" check-strictly
					:render-after-expand="false" :props="{ label: 'name', children: 'children' }" style="width: 100%" />
			</el-form-item>
			<el-form-item label="所在城市" prop="cityCode">
				<el-tree-select v-model="dataForm.cityCode" style="width: 100%" placeholder="" />
			</el-form-item>
			<el-form-item label="详细地址" prop="address">
				<el-input v-model="dataForm.address" placeholder="请输入"></el-input>
			</el-form-item>
			<el-form-item label="类型" prop="type">
				<el-tree-select v-model="dataForm.type" :data="typeList" value-key="id" check-strictly
					:render-after-expand="false" :props="{ label: 'name', children: 'children' }" style="width: 100%" />
			</el-form-item>
			<el-form-item label="邮政编码" prop="postal">
				<el-input v-model="dataForm.postal" placeholder="请输入"></el-input>
			</el-form-item>
			<el-form-item label="描述" prop="description">
				<el-input v-model="dataForm.description" placeholder="请输入" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { Ref, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useWorkplaceApi, useWorkplaceSubmitApi } from '@/api/nb/organization/workplace'
import { findListByDictId } from '@/api/nb/business/dictionaries_subset'

const emit = defineEmits(['refreshDataList'])

const typeList = ref([])
const visible = ref(false)
const dataFormRef = ref()
const dictionariesList = ref([])

const dataForm = reactive({
	id: '',
	workplace: '',
	regionCode: '',
	cityCode: '',
	address: '',
	type: '',
	postal: '',
	description: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getWorkplace(id)
	}

	getDictList('101', typeList)
	getDictList('110', dictionariesList)
}

const getDictList = (code: String, list: Ref) => {
	findListByDictId({ params: { code: code } }).then(res => {
		list.value = res.data
	})
}

const getWorkplace = (id: number) => {
	useWorkplaceApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useWorkplaceSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
